package com.sxsihe.heartmate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import org.w3c.dom.Text;

public class UserInfoDetailActivity extends Activity implements View.OnClickListener {

    LinearLayout name_layout,sex_layout,age_layout,phone_layout,ID_layout,linkman_layout,linkman_phone_layout;
    TextView name,sex,age,phone,ID,linkman,linkman_phone;
    ImageView back_icon;
    SharePreferenceUtil spu;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
         setContentView(R.layout.userinfo_detail);
        if (spu == null) {
            spu = SharePreferenceUtil.getInstance(this);
        }
        back_icon = (ImageView)findViewById(R.id.back_icon);
        back_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_left, R.anim.out_right);
            }
        });
        name = (TextView)findViewById(R.id.name);
        sex = (TextView)findViewById(R.id.sex);
        age = (TextView)findViewById(R.id.age);
        phone = (TextView)findViewById(R.id.phone);
        ID = (TextView)findViewById(R.id.ID);
        linkman = (TextView)findViewById(R.id.linkman);
        linkman_phone = (TextView)findViewById(R.id.linkman_phone);
        name_layout = (LinearLayout)findViewById(R.id.name_layout);
        name_layout.setOnClickListener(this);
        sex_layout = (LinearLayout)findViewById(R.id.sex_layout);
        sex_layout.setOnClickListener(this);
        age_layout = (LinearLayout)findViewById(R.id.age_layout);
        age_layout.setOnClickListener(this);
        phone_layout = (LinearLayout)findViewById(R.id.phone_layout);
        phone_layout.setOnClickListener(this);
        ID_layout = (LinearLayout)findViewById(R.id.ID_layout);
        ID_layout.setOnClickListener(this);
        linkman_layout = (LinearLayout)findViewById(R.id.linkman_layout);
        linkman_layout.setOnClickListener(this);
        linkman_phone_layout = (LinearLayout)findViewById(R.id.linkman_phone_layout);
        linkman_phone_layout.setOnClickListener(this);
        setInitValue();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
            case RESULT_OK:
                String value = data.getExtras().get("value").toString();
                String type = data.getExtras().get("type").toString();
                switch (type){
                    case "name":
                        name.setText(value);
                        spu.set("name",value);
                        break;
                    case "age":
                        age.setText(value);
                        spu.set("age", value);
                        break;
                    case "sex":
                        sex.setText(value);
                        spu.set("sex", value);
                        break;
                    case "ID":
                        ID.setText(value);
                        spu.set("ID", value);
                        break;
                    case "phone":
                        phone.setText(value);
                        spu.set("phone", value);
                        break;
                    case "linkman":
                        linkman.setText(value);
                        spu.set("linkman", value);
                        break;
                    case "linkman_phone":
                        linkman_phone.setText(value);
                        spu.set("linkman_phone", value);
                        break;
                }
                break;
            default:
                break;
        }
    }

    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.in_left, R.anim.out_right);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(UserInfoDetailActivity.this, UserInfoInputActivity.class);
        switch (v.getId()){
            case R.id.name_layout:
                intent.putExtra("type", "name");
                intent.putExtra("value",name.getText());
                break;
            case R.id.age_layout:
                intent.putExtra("type", "age");
                intent.putExtra("value",age.getText());
                break;
            case R.id.sex_layout:
                intent.putExtra("type", "sex");
                intent.putExtra("value",sex.getText());
                break;
            case R.id.ID_layout:
                intent.putExtra("type", "ID");
                intent.putExtra("value",ID.getText());
                break;
            case R.id.phone_layout:
                intent.putExtra("type", "phone");
                intent.putExtra("value",phone.getText());
                break;
            case R.id.linkman_layout:
                intent.putExtra("type", "linkman");
                intent.putExtra("value",linkman.getText());
                break;
            case R.id.linkman_phone_layout:
                intent.putExtra("type", "linkman_phone");
                intent.putExtra("value",linkman_phone.getText());
                break;
        }

        startActivityForResult(intent, 0);
        int version = Integer.valueOf(android.os.Build.VERSION.SDK);
        if(version  >= 5) {
            overridePendingTransition(R.anim.in_right, R.anim.out_left);
        }
    }

    private void setInitValue(){
        name.setText(spu.get("name",""));
        sex.setText(spu.get("sex",""));
        age.setText(spu.get("age","18"));
        spu.set("age","18");
        phone.setText(spu.get("phone",""));
        ID.setText(spu.get("ID",""));
        linkman.setText(spu.get("linkman",""));
        linkman_phone.setText(spu.get("linkman_phone",""));
    }
}
